﻿using DSAManager.Core;
using DSAManager.Models.Input;
using DSAManager.Models.Output;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DSAManager
{
    public partial class FrmSalesmanRecommend : Form
    {
        private PageRequest pageRequest;
        private int endpage = 0;
        public FrmSalesmanRecommend()
        {
            InitializeComponent();
        }

        void initQueryCond()
        {
            pageRequest = new PageRequest();
            txt_name.Clear();
            txt_mobile.Clear();
            dpt_time1.Checked = false;
            dpt_time2.Checked = false;
        }

        private void FrmSalesmanRecommend_Load(object sender, EventArgs e)
        {
            initQueryCond();
            loadSalesmanRecommendData(true);
        }

        public void loadSalesmanRecommendData(bool reload = false)
        {
            refreshPageRequest(reload);

            var data = Api.QueryRecommendInfo(pageRequest);
            listOrders.Items.Clear();
            if (data != null)
            {
                foreach (var info in data.data.Records)
                {
                    var item = listOrders.Items.Add(new TreeListViewItem(info.SalesmanName));
                    item.SubItems.Add(info.Mobile);
                    item.SubItems.Add(info.Address);
                    item.SubItems.Add(info.OpenId);
                    item.SubItems.Add(info.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    item.SubItems.Add(info.IsEnable ? "v" : "");
                    item.Tag = info;
                }

                handelPage(data.data);
            }
        }

        void handelPage(PageEntity<RecommendInfo> pageEntity)
        {
            if (pageEntity.PageCount > 1)
            {
                pan_page.Show();
                lbl_pageinfo.Show();
                lbl_pageinfo.Text = string.Format("共{0}条记录,当前显示{1}-{2}条记录", pageEntity.RecordCount, pageEntity.Begin, pageEntity.End);
                lbl_page.Text = pageEntity.PageNo + "/" + pageEntity.PageCount;
                btn_index.Enabled = pageEntity.PageNo != 1;
                btn_previous.Enabled = pageEntity.PageNo != 1;
                btn_next.Enabled = pageEntity.PageNo < pageEntity.PageCount;
                btn_end.Enabled = pageEntity.PageNo != pageEntity.PageCount;
                endpage = (int)pageEntity.PageCount;
            }
            else
            {
                pan_page.Hide();
                lbl_pageinfo.Hide();
            }
        }

        void refreshPageRequest(bool reload)
        {
            pageRequest.pageSize = 20;
            if (reload)
            {
                pageRequest = new PageRequest();
                pageRequest.pageSize = 20;
                pageRequest.pageNo = 1;

            }
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            pageRequest = new PageRequest();
            pageRequest.pageNo = 1;

            string cond = "";
            if (!string.IsNullOrEmpty(txt_name.Text.Trim()))
            {
                if (!string.IsNullOrEmpty(cond)) cond += "|";
                cond += "name:" + txt_name.Text.Trim();
            }

            if (!string.IsNullOrEmpty(txt_mobile.Text.Trim()))
            {
                if (!string.IsNullOrEmpty(cond)) cond += "|";
                cond += "mobile:" + txt_mobile.Text.Trim();
            }

            if (dpt_time1.Checked)
            {
                if (!string.IsNullOrEmpty(cond)) cond += "|";
                cond += "bt:" + new DateTime(dpt_time1.Value.Date.Ticks).ToString("yyyyMMddHHmmss");
            }

            if (dpt_time2.Checked)
            {
                if (!string.IsNullOrEmpty(cond)) cond += "|";
                cond += "et:" + new DateTime(dpt_time2.Value.Date.AddDays(1).AddMilliseconds(-1).Ticks).ToString("yyyyMMddHHmmss");
            }
            pageRequest.conds = cond;
            queryData();
        }

        void queryData()
        {
            loadSalesmanRecommendData();
        }

        private void btn_index_Click(object sender, EventArgs e)
        {
            pageRequest.pageNo = 1;
            queryData();
        }

        private void btn_previous_Click(object sender, EventArgs e)
        {
            pageRequest.pageNo -= 1;
            queryData();
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            pageRequest.pageNo += 1;
            queryData();
        }

        private void btn_end_Click(object sender, EventArgs e)
        {
            pageRequest.pageNo = endpage;
            queryData();
        }
    }
}
